package org.toy.wechat.gym.exception;

/**
 * @Description TODO
 * @Author devin
 * @Date: 2020/2/23 0023 11:16
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.toy.wechat.core.common.base.BaseResult;
import org.toy.wechat.core.common.base.BaseResultGenerator;
import org.toy.wechat.core.common.enums.BaseResultEnum;
import org.toy.wechat.core.common.exception.BusinessException;

/**
 * 全局Controller层异常处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionResolver {

    /**
     * 处理所有不可知异常
     *
     * @param e 异常
     * @return json结果
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseResult handleException(Exception e) {
        // 打印异常堆栈信息
        log.error(e.getMessage(), e);
        return BaseResultGenerator.error(BaseResultEnum.SERVER_ERROR);
    }

    /**
     * 处理所有业务异常
     *
     * @param e 业务异常
     * @return json结果
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public BaseResult handleOpdRuntimeException(BusinessException e) {
        // 不打印异常堆栈信息
        log.error(e.getErrorMessage());
        return BaseResultGenerator.error(e);
    }
}
